﻿using System.Text.RegularExpressions;
using Assets.Scripts.Net.Data.Model;
using Assets.Scripts.UI.Panel;
using UnityEngine.UI;

namespace Assets.Scripts.UI.Item
{
    /// <summary>
    /// 亲属信息的Item
    /// </summary>
    public class RelativeItem:UIBase<RelativeItem>
    {
        public InputField RelationText;
        public InputField NameText;
        public InputField CompanyText;
        public InputField TellNumberText;
        public Button DeleteButton;
        protected override UIType UIType
        {
            get { return UIType.Item;}
        }
        protected override void InitOnAwake()
        {
            AutoFindComponent(transform, true);
        }
        public void Init(RelativeInfo info)
        {
            RelationText.text = info.relationship;
            NameText.text = info.name;
            CompanyText.text = info.company;
            TellNumberText.text = info.phonenumber;
            RelationText.onEndEdit.AddListener(x =>
            {
                if (string.IsNullOrEmpty(x)) return;
                info.relationship = Regex.IsMatch(x, @"^[\u4e00-\u9fa5]+$") ? x : null;
                if (info.relationship == null)
                {
                    RelationText.text = null;
                    UIManager._Instance.ShowTipInfo("关系限定中文");
                }
            });
            NameText.onEndEdit.AddListener(x =>
            {
                if(string.IsNullOrEmpty(x))return;
                info.name = Regex.IsMatch(x, @"^[\u4e00-\u9fa5]+$") ? x : null;
                if (info.name==null)
                {
                    NameText.text = null;
                    UIManager._Instance.ShowTipInfo("名字限定中文");
                }
            });
            CompanyText.onEndEdit.AddListener(x =>
            {
                if (string.IsNullOrEmpty(x)) return;
                info.company = Regex.IsMatch(x, @"^[\u4e00-\u9fa5]+$") ? x : null;
                if (info.company == null)
                {
                    CompanyText.text = null;
                    UIManager._Instance.ShowTipInfo("公司名称限定中文");
                }
            });
            TellNumberText.onEndEdit.AddListener(x =>
            {
                if (string.IsNullOrEmpty(x))return;
                info.phonenumber = Regex.IsMatch(x, @"^1[3|4|5|8][0-9]\d{8}$") ? x : null;
                if (info.phonenumber == null)
                {
                    TellNumberText.text = null;
                    UIManager._Instance.ShowTipInfo("请输入正确的手机号");
                }
            });
            DeleteButton.onClick.AddListener(() =>
            {
                StudentInfoPanel._Instance.RemoveRelativeInfo(info);
                Destroy(gameObject);
            });
        }
    }
}